﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using Microsoft.Kinect;

namespace Kinect.Toolbox.Wpf
{
    public class CursorWpf :MouseController
    {
        private Point _cursorPos;
        public Point CursorPos
        {
            get { return _cursorPos; }
        }
        private CursorAdorner _cursorAdorner;

        private readonly Window _window;

        private static bool _isInitialized;
        private static CursorWpf _instance;


        /// <summary>
        /// Gets or sets a value indicating whether this instance has hand threshold.
        /// </summary>
        /// <value>
        /// 	<c>true</c> if this instance has hand threshold; otherwise, <c>false</c>.
        /// </value>
        private bool _hasHandThreshold = true;
        public bool HasHandThreshold
        {
            get { return _hasHandThreshold; }
            set { _hasHandThreshold = value; }
        }

        /// <summary>
        /// Creates the specified window.
        /// </summary>
        /// <param name="window">The window.</param>
        public static void Create(Window window)
        {
            if (!_isInitialized)
            {
                _instance = new CursorWpf(window,KinectSensor.KinectSensors[0],null);
                _isInitialized = true;
            }
        }

        /// <summary>
        /// Creates the specified window.
        /// </summary>
        /// <param name="window">The window.</param>
        /// <param name="cursor">The cursor.</param>
        public static void Create(Window window, FrameworkElement cursor)
        {
            if (!_isInitialized)
            {
                _instance = new CursorWpf(window, KinectSensor.KinectSensors[0], cursor);
                _isInitialized = true;
            }
        }

        /// <summary>
        /// Creates the specified window.
        /// </summary>
        /// <param name="window">The window.</param>
        /// <param name="sensor">The sensor.</param>
        public static void Create(Window window, KinectSensor sensor)
        {
            if (!_isInitialized)
            {
                _instance = new CursorWpf(window, sensor,null);
                _isInitialized = true;
            }
        }

        /// <summary>
        /// Creates the specified window.
        /// </summary>
        /// <param name="window">The window.</param>
        /// <param name="sensor">The sensor.</param>
        /// <param name="cursor">The cursor.</param>
        public static void Create(Window window, KinectSensor sensor, FrameworkElement cursor)
        {
            if (!_isInitialized)
            {
                _instance = new CursorWpf(window, sensor, cursor);
                _isInitialized = true;
            }
        }

        /// <summary>
        /// Gets the instance.
        /// </summary>
        public static CursorWpf Instance
        {
            get { return _instance; }
        }


        public CursorWpf(Window window, KinectSensor sensor, FrameworkElement cursor)
            : base((int)window.ActualWidth, (int)window.ActualHeight)
        {
            // ensure kinects are present
            this._window = window;
            _cursorPos = new Point();
            _window.Unloaded += delegate
            {
                //if (this._kinectSensor.SkeletonStream.IsEnabled)
                //    this._kinectSensor.SkeletonStream.Disable();
                //_kinectSensor.Stop();
            };

            _window.Loaded += delegate
            {

                _cursorAdorner = new CursorAdorner((FrameworkElement)window.Content);

            };
             
        }


    }
}
